package space.xxhui.service

import org.apache.log4j.Logger
import org.apache.spark.rdd.RDD
import space.xxhui.config.MyBatisConfig
import space.xxhui.dao.WordFreqDao
import space.xxhui.model.WordFreq
import space.xxhui.utils.DateUtils


/**
 * @author HitvzByHui
 *         date 2021-09-12
 */
object WordFreqService extends Serializable {

  @transient lazy val log = Logger.getLogger(this.getClass)

  def save(rdd: RDD[(String, Int)]) = {
    if (!rdd.isEmpty) {
      rdd.foreachPartition(partitionRecords => {
        val date = DateUtils.timeStamp2Date(System.currentTimeMillis(), DateUtils.FORMAT_DATE)
        partitionRecords.foreach(record => {
          val db = MyBatisConfig.persistenceContext
          db.transaction { implicit seeesion =>
            WordFreqDao incrInsert WordFreq(record._1, date, record._2)
            println(record._1,record._2)
          }
        })
      })
    }
  }


}
